package es.uoc.mxpdroid.algorithm;

import es.uoc.mxpdroid.entity.RasterLayer;

public class CombineMaskAlgorithm {

   private RasterLayer grid;
   private RasterLayer maskGrid;
   private RasterLayer result;

   public CombineMaskAlgorithm(RasterLayer layer1, RasterLayer mask) {

      grid = layer1;
      maskGrid = mask;
      
      //initialize result matrix
      result = new RasterLayer(layer1.getLayerType());
      result.initFromLayer(layer1);
      
      processAlgorithm();
   }


   private void processAlgorithm()  {

      int x, y;
      int iNX, iNY;
      double dCellValue, mask;


      iNX = grid.getNX();
      iNY = grid.getNY();

      for (y = 0; y < iNY; y++) {
         for (x = 0; x < iNX; x++) {
            dCellValue = grid.getCellValueAsDouble(x, y);
            mask = maskGrid.getCellValueAsDouble(x, y);
            if (maskGrid.isNoDataValue(mask))
            	result.setCellValue(x, y, dCellValue);
         }
      }
   }
   
   public RasterLayer getResult(){
	   return result;
   }


}
